//
//  API_Task.swift
//  WalkNovel
//
//  Created by Kevin on 2023/10/8.
//

import UIKit
import Moya

enum API_Task {
    /// 获取任务
    case getTask
    /// 完成任务
    case completeTask(Int)
}

let task_timeoutClosure = { (endpoint: Endpoint, done: @escaping MoyaProvider<API_Task>.RequestResultClosure) in
    do {
        var request = try endpoint.urlRequest()
        request.timeoutInterval = 5
        done(.success(request))
    }catch {
        return
    }
}

let provider_task = MoyaProvider<API_Task>(requestClosure: task_timeoutClosure)

extension API_Task: TargetType {
    var baseURL: URL {
        return URL(string: server.APIHost)!
    }
    
    var path: String {
        switch self {
        case .getTask:
            return "/Shield/getUserTask"
        case .completeTask(_):
            return "/Shield/receiveReward"
        }
    }
    
    var method: Moya.Method {
        return .post
    }
    
    var parameterEncoding: ParameterEncoding {
        return JSONEncoding.default
    }
    
    var sampleData: Data {
        return "test".data(using: String.Encoding.utf8)!
    }
    
    var task: Task {
        var ps: [String: Any] = ["ver": getAPPVersion(), "channel": "ios", "third_channel": "apple", "timestamp": Date().timeStamp, "userId": getUser()?.userId ?? "", "uuid": getKeyUUId(), "user_channel": getADID()]
        switch self {
        case .getTask:
            break
        case .completeTask(let taskId):
            ps["taskId"] = taskId
        }
        return .requestParameters(parameters: ps, encoding: JSONEncoding.default)
    }
    
    var headers: [String : String]? {
        return ["content-type": "application/json; charset=UTF-8"]
    }
}
